// 获取全局应用程序实例对象
const app = getApp()

//判断车牌号
function isVehicleNumber(vehicleNumber) {
  var result = false;
  if (vehicleNumber.length == 7) {
    var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
    result = express.test(vehicleNumber);
  }
  return result;
}

Page({
  data: {
    plate_number: '',
    brand: '',
    color: '',
    linkname: '',
    phone: '',
    models: ['SUV', '轿车', '商务车',],
    types: '',
  },
  onLoad: function () {
    var that = this;
    var params = {
      openid: app.globalData.info.openid,
    };
    //发起请求  
    app.network.POST(
      'https://www.ausck.com/api/get_car_info',
      {
        params: params,
        success: function (res) {
          //拿到解密后的数据，进行代码逻辑 
          if (res.data.status == 200) {
            that.setData({
              plate_number: res.data.data.plate_number || '',
              brand: res.data.data.brand || '',
              color: res.data.data.color || '',
              linkname: res.data.data.linkname || '',
              types: res.data.data.types || 0
            })
            if (res.data.data.phone) {
              that.setData({
                phone: res.data.data.phone
              })
            }
            else {
              //发起请求  
              app.network.POST(
                'https://www.ausck.com/api/vip_info',
                {
                  params: params,
                  success: function (res) {
                    //拿到解密后的数据，进行代码逻辑 
                    that.setData({
                      phone: res.data.data.phone
                    })
                  }
                })
            }
          }
        },
        fail: function () {
          //失败后的逻辑  
        }
      })
  },
  //车牌
  plateNumberInput: function (e) {
    this.setData({
      plate_number: e.detail.value
    })
  },
  //车型
  brandInput: function (e) {
    this.setData({
      brand: e.detail.value
    })
  },
  typesChange: function (e) {
    this.setData({
      types: e.detail.value
    })
  },
  //颜色
  colorInput: function (e) {
    this.setData({
      color: e.detail.value
    })
  },
  //联系人
  linknameInput: function (e) {
    this.setData({
      linkname: e.detail.value
    })
  },
  //手机号
  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 监听picker选择器
   */
  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value
    });
  },

  //报错
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },

  //确认按钮
  formSubmit: function (e) {
    var params = e.detail.value;
    params.openid = app.globalData.info.openid;

    setTimeout(() => {

      var re = /^1\d{10}$/;    //手机号正则表达式

      if (!isVehicleNumber(params.plate_number)) {
        console.log(params.plate_number)
        this.setData(
          { popErrorMsg: "车牌号填写错误,请注意大写字母！" }
        );
      }
      else if (!params.brand) {
        this.setData(
          { popErrorMsg: "品牌填写错误！" }
        );
      }
      else if (!params.color) {
        this.setData(
          { popErrorMsg: "车辆颜色未填写!" }
        );
      }
      else if (!params.linkname) {
        this.setData(
          { popErrorMsg: "联系人未填写!" }
        );
      }
      else if (!re.test(params.phone)) {
        this.setData(
          { popErrorMsg: "手机号填写错误!" }
        );
      } else {
        //发起请求  
        app.network.POST(
          'https://www.ausck.com/api/car_info',
          {
            params: params,
            success: function (res) {
              console.log(res)
              //拿到解密后的数据，进行代码逻辑
              if (res.data.status == 200) {
                wx.navigateTo({
                  url: "/pages/place-order/place-order"
                })
              }
            },
            fail: function () {
              //失败后的逻辑  

            },
          })
        return;
      }
      this.ohShitfadeOut();
      return;
    }, 100)
  },
  //取消按钮
  toIndex: function () {
    wx.navigateTo({
      url: "/pages/index/index"
    })
  }
})
